package com.fowo.api.service.handleWork;

import com.fowo.api.entity.QualityCheckStorage;
import com.fowo.api.mapper.QualityCheckStorageMapper;
import com.fowo.api.model.quality.check.storage.QualityCheckStorageVo;
import com.fowo.api.service.impl.QualityCheckStorageBaseServiceImpl;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class QualityCheckStorageDServiceImpl {

    @Resource
    private QualityCheckStorageMapper qualityCheckStorageMapper;

    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate(List<QualityCheckStorageVo> models) throws Exception {
        JwtUserInfo currentUser = JwtUserInfo.fromHeader();
        for(QualityCheckStorageVo model : models){
            QualityCheckStorage entity = qualityCheckStorageMapper.selectById(model.getId());
            if(entity == null) {
                throw new Exception("质检单不存在");
            }
            BeanUtils.copyProperties(model, entity, "id", "productImg", "createTime", "createUser", "lastUpdateTime", "lastUpdateUser");
            entity.setProductGoodNum(model.getAllProductGoodNum());
            entity.setProductBadNum(model.getAllProductBadNum());
            entity.setQcStatus("1");
            entity.setLastUpdateTime(new Date());
            if (currentUser != null) {
                entity.setLastUpdateUser(currentUser.getUserId());
            }
            qualityCheckStorageMapper.updateById(entity);
        }
    }
}
